package org.sample.programs.ui.phone;


import org.sample.programs.R;
import org.sample.programs.domain.Library;
import org.sample.programs.domain.Video;
import org.sample.programs.service.task.GetYouTubeUserVideosTask;
import org.sample.programs.service.task.GetYouTubeUserVideosTaskCategory;
import org.sample.programs.ui.VideoClickListener;
import org.sample.programs.ui.widget.VideosListView;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class VideoWithCustomDialog extends Activity implements VideoClickListener
{
    // A reference to our list that will hold the video details
	private VideosListView listView;
	
	// This is where you have to specify the youtube username 
	private String username = "mycookinggalaxy";

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category_activity_main); 
        new Thread(new GetYouTubeUserVideosTaskCategory(responseHandler, username)).start();
        listView = (VideosListView) findViewById(R.id.videosListView);
        listView.setOnVideoClickListener(this);
    }
    
	Handler responseHandler = new Handler() {
		public void handleMessage(Message msg) {
			populateListWithVideos(msg);
		};
	};

	private void populateListWithVideos(Message msg) {
		Library lib = (Library) msg.getData().get(GetYouTubeUserVideosTask.LIBRARY);
		listView.setVideos(lib.getVideos());
	}
	
	@Override
	protected void onStop() {
		responseHandler = null;
		super.onStop();
	}

	public void onVideoClicked(Video video) 
	{
		/*String url = video.getUrl();
		Uri uri = Uri.parse(url);
		String videoId = uri.getQueryParameter("v");
		Intent lVideoIntent = new Intent(null, Uri.parse("ytv://" + videoId), this,OpenYouTubePlayerActivity.class);
		startActivity(lVideoIntent);*/
		

		final Dialog dialog = new Dialog(VideoWithCustomDialog.this);
		dialog.setContentView(R.layout.maindialog);
		dialog.setTitle("This is my custom dialog box");
		dialog.setCancelable(true);
		// there are a lot of settings, for dialog, check them all out!

		// set up text
		TextView text = (TextView) dialog.findViewById(R.id.TextView01);
		text.setText(" This is just an sample custom dialog ");

		// set up image view
		ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
		img.setImageResource(R.drawable.ic_launcher);

		// set up button
		Button button = (Button) dialog.findViewById(R.id.Button01);
		button.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				dialog.dismiss();
			}
		});
		// now that the dialog is set up, it's time to show it
		dialog.show();
	
	}
}